package com.ynet.middleground.user.service.impl;

import java.util.List;

import javax.validation.Valid;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.base.Throwables;
import com.ynet.core.common.BaseReqObjPager;
import com.ynet.core.common.ServiceResult;
import com.ynet.core.exception.BusinessException;
import com.ynet.core.log.IfpLogger;
import com.ynet.middleground.user.bean.*;
import com.ynet.middleground.user.common.ErrorMessageConstant;
import com.ynet.middleground.user.dto.ChannelCompanyDTO;
import com.ynet.middleground.user.model.ChannelCompanyModel;
import com.ynet.middleground.user.service.ChannelCompanyService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * @author liulx
 * @description 渠道关联公司管理服务
 * @date 2020-07-02 17:13
 */
@Api(value = "渠道关联公司管理服务", tags = {"渠道关联公司管理服务"})
@Produces({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8"})
@Consumes({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8"})
@Path("channel-company")
@Service(timeout = 6000, version = "0.0.1")
public class ChannelCompanyServiceImpl implements ChannelCompanyService, GenericService {

    @Autowired
    private ChannelCompanyModel channelCompanyModel;

    /**
     * 查询渠道对应公司列表
     *
     * @param req 查询渠道详情请求对象
     * @return 渠道关联公司信息列表
     */
    @ApiOperation(value = "查询渠道对应的公司列表", notes = "查询渠道对应的公司列表")
    @ApiImplicitParam(name = "requestObj", dataType = "QueryChannelReq", paramType = "body", required = true)
    @POST
    @Override
    public ServiceResult<List<ChannelCompanyDTO>> listByChannelCode(@Valid QueryChannelReq req) {
        ServiceResult<List<ChannelCompanyDTO>> result = new ServiceResult<>();
        try {
            result.setResult(channelCompanyModel.companyListByChannelCode(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "查询渠道对应公司列表服务异常，case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return result;
        }
        return result;
    }

    /**
     * 分页查询渠道关联的公司列表
     *
     * @param req 基础参数分页
     * @return 渠道关联公司列表
     */
    @ApiOperation(value = "分页查询渠道关联的公司列表", notes = "分页查询渠道关联的公司列表", hidden = true)
    @Override
    public ServiceResult<List<ChannelCompanyDTO>> listPage(@Valid BaseReqObjPager req) {
        ServiceResult<List<ChannelCompanyDTO>> result = new ServiceResult<>();
        try {
            result.setResult(channelCompanyModel.listPage(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "分页查询渠道关联的公司列表服务异常，case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return result;
        }
        result.setPagerInfo(req.getPagerInfo());
        return result;
    }

    /**
     * 添加关联公司
     *
     * @param req 添加渠道关联公司请求对象
     * @return 添加成功后的公司 id
     */
    @ApiOperation(value = "添加关联公司", notes = "添加关联公司", hidden = true)
    @Override
    public ServiceResult<Integer> addCompany(@Valid AddChannelCompanyReq req) {
        ServiceResult<Integer> result = new ServiceResult<>();
        try {
            result.setResult(channelCompanyModel.add(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "添加关联公司服务异常，case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return result;
        }
        return result;
    }

    /**
     * 根据关联公司 id 查询详情
     *
     * @param req 查询基础对象
     * @return 渠道关联公司信息传输对象
     */
    @ApiOperation(value = "查询关联公司详情", notes = "查询关联公司详情", hidden = true)
    @Override
    public ServiceResult<ChannelCompanyDTO> queryDetailById(@Valid QueryBaseReq req) {
        ServiceResult<ChannelCompanyDTO> result = new ServiceResult<>();
        try {
            result.setResult(channelCompanyModel.getDetailById(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "查询关联公司详情服务异常，case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return result;
        }
        return result;
    }

    /**
     * 根据关联公司代码查询详情
     *
     * @param req 查询渠道公司关系详情请求对象
     * @return 渠道关联公司信息传输对象
     */
    @ApiOperation(value = "根据关联公司代码查询详情", notes = "根据关联公司代码查询详情", hidden = true)
    @Override
    public ServiceResult<ChannelCompanyDTO> getDetailByCompanyCode(@Valid QueryChannelCompanyReq req) {
        ServiceResult<ChannelCompanyDTO> result = new ServiceResult<>();
        try {
            result.setResult(channelCompanyModel.getDetailByCompanyCode(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "根据关联公司代码查询详情服务异常，case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return result;
        }
        return result;
    }

    /**
     * 更新渠道公司关联信息
     *
     * @param req 修改渠道关联公司请求对象
     * @return 结果以 success 字段为准, true 为修改成功， false 为修改失败
     */
    @ApiOperation(value = "更新渠道公司关联信息", notes = "更新渠道公司关联信息", hidden = true)
    @Override
    public ServiceResult<Object> modify(@Valid UpdateChannelCompanyReq req) {
        ServiceResult<Object> result = new ServiceResult<>();
        try {
            channelCompanyModel.modify(req);
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "更新渠道公司关联信息服务异常，case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return result;
        }
        return result;
    }

    /**
     * 删除关联公司信息
     *
     * @param req 查询基础对象
     * @return 结果以 success 字段为准, true 为删除成功， false 为删除失败
     */
    @ApiOperation(value = "删除关联公司信息", notes = "删除关联公司信息", hidden = true)
    @Override
    public ServiceResult<Object> delete(@Valid QueryBaseReq req) {
        ServiceResult<Object> result = new ServiceResult<>();
        try {
            channelCompanyModel.deleteCompany(req);
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "删除关联公司信息服务异常，case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return result;
        }
        return result;
    }

}
